package com.example.myapplication.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

/**
 * @Author: Ng
 * @Date: 2024/4/8
 * @Versopn: 1.0.0
 */
public class NewsMenuAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;
    private String[] titles;

    public NewsMenuAdapter(@NonNull FragmentManager fm, List<Fragment> fragments, String[] titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    /**
     * 返回当前的 Fragment
     * @param position: 当前页面的位置
     */
    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    /**
     * Fragment 中的个数
     */
    @Override
    public int getCount() {
        return fragments.size();
    }

    /**
     * 返回当前标题
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
